package io.renren.modules.work.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.ConfigConstant;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.work.entity.ScContractEntity;
import io.renren.modules.work.service.ScContractService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.work.entity.ScCompanyEntity;
import io.renren.modules.work.service.ScCompanyService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 客户
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-05-08 23:01:13
 */
@RestController
@RequestMapping("work/sccompany")
public class ScCompanyController extends AbstractController {
    @Autowired
    private ScCompanyService scCompanyService;

    @Autowired
    private ScContractService sccontractService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("work:sccompany:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scCompanyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{companyid}")
    @RequiresPermissions("work:sccompany:info")
    public R info(@PathVariable("companyid") String companyid){
		ScCompanyEntity scCompany = scCompanyService.getById(companyid);

        return R.ok().put("scCompany", scCompany);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ScCompanyEntity scCompany){
        SysUserEntity user = getUser();
        scCompany.setCremanid(user.getUserId().toString());
        scCompany.setCremanname(user.getUsername());
        scCompany.setCretime(new Date());
        scCompany.setUpdtime(new Date());
        scCompany.setCompanyno(ConfigConstant.generateCompanyNo());
		scCompanyService.save(scCompany);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("work:sccompany:update")
    public R update(@RequestBody ScCompanyEntity scCompany){
		scCompanyService.updateById(scCompany);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] companyids){
		scCompanyService.removeByIds(Arrays.asList(companyids));
        //删除细单
        for (String companyid : companyids) {
            QueryWrapper<ScContractEntity> scContractEntityQueryWrapper = new QueryWrapper<>();
            scContractEntityQueryWrapper.eq("companyid",companyid);
            sccontractService.remove(scContractEntityQueryWrapper);
        }
        return R.ok();
    }

}
